/*
 * 	Character Count Plugin - jQuery plugin
 * 	Dynamic character count for text areas and input fields
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/7161/jquery-plugin-simplest-twitterlike-dynamic-character-count-for-textareas
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 *	Rewritted by <ytukie@gmail.com>
 */
 
(function($) {

	$.fn.charCount = function(options){
	  
		// default configuration properties
		var defaults = {	
			allowed: 200,		
			warning: 25,
			counterId: 'id_span_counter',
			textareId: 'message',
			cssWarning: 'cls_charCount_warning',
			cssExceeded: 'cls_charCount_exceeded'
		};
			
		var options = $.extend(defaults, options); 
		var counter = '#' + options.counterId;
		var textare = '#' + options.textareId;
		
		$(counter).html(options.allowed);
		doCount();
		
		function doCount() {
			var count = $(textare).val().length;
			var available = options.allowed - count;
			
			if(available <= options.warning && available >= 0){
				// $(obj).next().addClass(options.cssWarning);
				// 有警告
				$(counter).addClass(options.cssWarning);
			} else {
				// $(obj).next().removeClass(options.cssWarning);
				// 无警告
				$(counter).removeClass(options.cssWarning);
			}
			if(available < 0){
				// $(obj).next().addClass(options.cssExceeded);
				// 超出字数
				$(counter).addClass(options.cssExceeded);
			} else {
				// $(obj).next().removeClass(options.cssExceeded);
				// 正常
				$(counter).removeClass(options.cssExceeded);
			}
			// $(obj).next().html(options.counterText + available);
			// 显示
			$(counter).html(available);
		}
		
		$(textare).bind('keyup keydown change', function(){
			doCount();
		});
		
	};

})(jQuery);
